<?php
/**
 *
 * 版权所有：忠赢网站管理系统<www.91zoy.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;

class PcategoryController extends ComController
{

    public function index()
    {
        $pcategory = M('pcategory')->field('id,title,o')->order('o asc')->select();
        $pcategory = $this->getMenu($pcategory);
        $this->assign('pcategory', $pcategory);
        $this->display();
    }

    public function del()
    {

        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $data['id'] = $id;
            $pcategory = M('pcategory');
            if ($pcategory->where('cid=' . $id)->count()) {
                die('2');//存在子类，严禁删除。
            } else {
                $pcategory->where('id=' . $id)->delete();
                addlog('删除分类，ID：' . $id);
            }
            die('1');
        } else {
            die('0');
        }

    }

    public function edit()
    {

        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        $currentcategory = M('pcategory')->where('id=' . $id)->find();
        $this->assign('currentcategory', $currentcategory);

        $pcategory = M('pcategory')->field('id,name')->where("id <> {$id}")->order('o asc')->select();
        $tree = new Tree($pcategory);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $pcategory = $tree->get_tree(0, $str, $currentcategory['cid']);

        $this->assign('pcategory', $pcategory);
        $this->display('form');
    }

    public function add()
    {

        $cid = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
        $pcategory = M('pcategory')->field('id,title')->order('o asc')->select();
        $tree = new Tree($pcategory);
        $str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
        $pcategory = $tree->get_tree(0, $str, $cid);
        $this->assign('pcategory', $pcategory);
        $this->display('form');
    }

    public function update($act = null)
    {
        if ($act == 'order') {
            $id = I('post.id', 0, 'intval');
            if (!$id) {
                die('0');
            }
            $o = I('post.o', 0, 'intval');
            M('pcategory')->data(array('o' => $o))->where("id='{$id}'")->save();
            addlog('分类修改排序，ID：' . $id);
            die('1');
        }

        $id = I('post.id', false, 'intval');
        $data['cid'] = I('post.cid', 0, 'intval');
        $data['title'] = I('post.title');
        $data['dir'] = I('post.dir','',array('strip_tags','trim'));
        $data['o'] = I('post.o', 0, 'intval');
        $data['ctime'] = date('Y-m-d H:i:s');
        if ($data['title'] == '') {
            $this->error('分类名称不能为空！');
        }
        if ($id) {
            if (M('pcategory')->data($data)->where('id=' . $id)->save()) {
                addlog('文章分类修改，ID：' . $id . '，名称：' . $data['name']);
                $this->success('恭喜，分类修改成功！',U('/Qwadmin/Pcategory/index'),3);
                die(0);
            }
        } else {
            $id = M('pcategory')->data($data)->add();
            if ($id) {
                addlog('新增分类，ID：' . $id . '，名称：' . $data['name']);
                $this->success('恭喜，新增分类成功！',U('/Qwadmin/Pcategory/index'),3);
                die(0);
            }
        }
        $this->success('恭喜，操作成功！');
    }
}
